<?php
namespace App\Observers;

use App\Model\Banner;

// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

class PictureObserver
{
    //错误消息
    protected $error = [];
    //图片上传路径
    public $path  = '';

    //处理图片上传
    public function handleUploadedImage($image)
    {
        if (!is_null($image)) {
            $validArr = ['png','jpg','jpeg','gif'];
            if (!in_array($image->getClientOriginalExtension(),$validArr)){
                $this->error[] = '不支持的图片格式';
                return false;
            }
            $path = $image->store('uploads'.'/'.date('Y-m-d'));
            return $this->path = $path;
        }
        $this->error[] = '未选择上传文件';
        return false;
    }

    //获取第一个错误消息
    public function getFirstError()
    {
        return count($this->error)>0 ? $this->error[0] : "";
    }
}